package marsdonne.demo4gorm

import grails.gorm.multitenancy.Tenant
import grails.gorm.multitenancy.Tenants
import grails.gorm.multitenancy.WithoutTenant
import marsdonne.demo4gorm.domains.Book
import marsdonne.demo4gorm.domains.Person
import org.grails.datastore.mapping.mongo.MongoDatastore
import org.grails.orm.hibernate.HibernateDatastore
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner)
@SpringBootTest(classes = Demo4gormApplication.class)
class Demo4gormApplicationTests {

    @Test
    void testPersonSave() {
        new Person(firstName: "mars", lastName: "donne").save(flush: true)

        assert Person.count() == 1
    }

    @Test
    void testPersonValidator() {
        def person = new Person(lastName: "donne")
        person.save(flush: true)

        assert person.hasErrors()
    }

    @Test
    @Tenant({ "tenant1" })
    void testPersonWithTenant1() {
        def person = new Person(lastName: "donne")
        person.save(flush: true)

        assert person.hasErrors()
        assert Tenants.currentId(HibernateDatastore) == "tenant1"
        assert Person.count == 1
    }

    @Test
    @WithoutTenant
    void testBookSave() {
        new Book(name: "123", author: "mars").save(flush: true)

        assert Tenants.currentId(MongoDatastore) == 'DEFAULT'
        assert Book.count() == 1
    }
}
